package game.Actors;

import game.Config.Config;

import org.newdawn.slick.Image;
import org.newdawn.slick.geom.Vector2f;

/**
 * This class represents a movable actor in the game.
 * This class extends the Actor class, and thus has
 * an image and a position.
 * When a MovableActor is updated, it also moves through
 * the screen with a direction and speed.
 * @author Sebastian Isheden and Victor Beer
 *
 */
public abstract class MovableActor extends Actor{
	protected float speed;
	protected Vector2f direction = new Vector2f(0,0);	
	/**
	 * Constructor for MovableActor. Creates a MovableActor and 
	 * sets its starting position and image.
	 * Calling this constructor will not set the
	 * speed or direction of the MovableActor, and thus
	 * the object will not move when updated.
	 * The speed and direction can be changed by the
	 * methods setSpeed and setDirection.
	 * @param image	The image of the MovableActor.
	 * @param position The starting position of the MovableActor.
	 */
	public MovableActor(Image image, Vector2f position) {
		super(image, position);
	}
	/**
	 * Constructor for MovableActor. Creates a MovableActor and
	 * sets its starting position, image, speed and direction.
	 * Calling this constructor will make the actor move in 
	 * the given direction, with given speed.
	 * The speed and direction can also be changed by
	 * the methods setSpeed and setDirection
	 * @param image The image of the MovableActor.
	 * @param position The starting position of the MovableActor.
	 * @param speed The speed of the MovableActor, how fast the object will traverse the screen.
	 * @param direction The direction of the MovableActor, in which direction the object will traverse the screen.
	 */
	public MovableActor(Image image, Vector2f position,float speed, Vector2f direction){
		super(image,position);
		this.speed = speed;
		this.direction = new Vector2f(direction);
		direction.normalise();
	}
	
	/**
	 * Sets the speed of this actor.
	 * @param speed	The speed of the actor.
	 */
	public void setSpeed(float speed){
		this.speed = speed;
	}
	/**
	 * Sets the direction of the actor. If the direction.length() > 1,
	 * the direction is automatically normalized.
	 * @param direction	The direction of the actor.
	 */
	public void setDirection(Vector2f direction){
		this.direction = new Vector2f(direction).normalise();
	}
	/**
	 * Sets the direction of the actor. If the direction.length() > 1,
	 * the direction is automatically normalized.
	 * @param x	The x direction of the actor.
	 * @param y The y direction of the actor.
	 */
	public void setDirection(float x, float y){
		this.direction.x = x;
		this.direction.y = y;
		this.direction.normalise();
	}
	/**
	 * Updates the position of the MoveableActor.
	 */
	protected void update(int delta){
		this.position.add(new Vector2f(direction).scale(speed*delta/Config.MILLISECONDS));
	}
	protected void setImageRotation(){
		this.image.setRotation((float)direction.getTheta());
	}
	
}
